//
//  YTSearchController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/2.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTSearchController.h"
#import "YTHomeArticleCell.h"

NSString *HomeSeacherCellID = @"HomeSeacherCellID";
@interface YTSearchController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UISearchBar *searchBar;

@property(nonatomic ,strong)YTHomeSearchModel *model;


@end

@implementation YTSearchController

// lazy searchBar
- (UISearchBar *)searchBar
{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        _searchBar.placeholder = self.keyWord;
        _searchBar.clearsContextBeforeDrawing = YES;
        _searchBar.delegate = self;
        
    }
    return _searchBar;
}

// tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
 
        // setupRefreshHeader RefreshFooter
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self loadDataWithPullup:NO];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadDataWithPullup:YES];
        }];
        
        [footer setTitle:@"没有更多了哦～" forState:MJRefreshStateNoMoreData];
        [footer setTitle:@"" forState:MJRefreshStateIdle];
        header.lastUpdatedTimeLabel.hidden = YES;
        
        
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        _tableView.rowHeight = 100;
        
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 注册可重cell
        [_tableView registerNib:[UINib nibWithNibName:@"YTHomeArticleCell" bundle:nil]
         forCellReuseIdentifier:HomeSeacherCellID];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.view addSubview:self.tableView];
    
    [self setupUI];
}

#pragma mark: - 设置界面
- (void)setupUI
{
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.titleView = self.searchBar;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    
    [self.tableView.mj_header beginRefreshing];
    
    
}

#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - 加载数据
- (void)loadDataWithPullup:(BOOL)pullup
{
    int page = self.model.page ? self.model.page.intValue : 1;
    
    // 下来刷新 page++
    if (pullup) {
        page++;
        // 判断是否还有数据
        if (self.model.list.count == self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        page = 1;
    }
    
    [YTNetworkManager.shareManager requestSeachWithKeyWord:self.keyWord
                                                     paged:page
                                                 page_size:@"10"
                                                completion:^(YTHomeSearchModel *model, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:error];
            return ;
        }
        
        // 判断是否搜到数据
        if (model.list.count == 0) {
            [self.view showTipWithTipStr:@"搜索不到相关内容哦~"];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            return;
        }
        
        if (!pullup) {
            self.model = model;
            [self.tableView.mj_header endRefreshing];
        }else{
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
    }];
    
}


#pragma mark: -  table view dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTHomeArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeSeacherCellID forIndexPath:indexPath];
    
    cell.searchModel = self.model.list[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark: - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model.list[indexPath.row].isArticle) {
        YTHomeDetailController *detailVC = [[YTHomeDetailController alloc] init];
        detailVC.ID = self.model.list[indexPath.row].ID;
        [self.navigationController pushViewController:detailVC animated:true];
    }else{
        YTDetailNewController *detailNew = [YTDetailNewController controller];
        detailNew.ID = self.model.list[indexPath.row].ID;
        [self.navigationController pushViewController:detailNew animated:true];
    }
}

#pragma mark: - searchBar delegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.keyWord = searchBar.text;
    
    [self.tableView.mj_header beginRefreshing];
}


@end
